#! /usr/bin/env node
import { program } from 'commander';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import chalk from 'chalk';

const require = createRequire(import.meta.url);

// 获取当前文件所在的绝对路径
const __filename = fileURLToPath(import.meta.url);

// 获取当前目录路径
const __dirname = dirname(__filename);

const packageJson = require(join(__dirname, '../package.json'));

program.version(`my-cli@${packageJson.version}`).usage('<command> [options]');

program
  .command('create <app-name>')
  .description('创建一个vue3项目')
  .option('-f, --force', 'overwrite target directory')
  .action(async (name, option) => {
    (await import('./commands/create.js')).default(name, option);
  });

program
  .command('config [value]')
  .description('设置配置文件')
  .option('-q --get <path>', 'get value')
  .option('-s --set <path> <value>', 'set value')
  .option('-d --delete <path>', 'delete value')
  .action(async (value, option) => {
    (await import('./commands/config.js')).default(value, option);
  });

program.addHelpText(
  'after',
  `\nRun ${chalk.blueBright(
    'my-cli <command> --help'
  )}  for detailed usage of given command.`
);

// process.argv 用户传递的参数
// 解析用户参数自动提供 --help -h 选项在终端
program.parse(process.argv);
